package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//用户的增删改查
public class UserDao {
    //1.新增用户（注册）
    //把一个User对象插入数据库中
    void add(User user) {
        //1.建立连接
        Connection connection = DBUtil.getConnection();
        //2.拼装SQL语句
        String sql = "insert into user values (null,?,?)";

        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,user.getName());
            statement.setString(2,user.getPassword());
            //3.执行SQL语句
            int ret = statement.executeUpdate();
            if(ret != 1){
                System.out.println("插入新用户失败");
                return;
            }
            System.out.println("插入新用户成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.释放连接
            DBUtil.close(connection,statement,null);
        }
    }

    //2.按照名字查找用户（登录）
    public User selectByName(String name){
        //1.建立连接
        Connection connection = DBUtil.getConnection();
        //2.拼装sql语句
        String sql = "select * from user where name = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            //3.执行sql
            resultSet = statement.executeQuery();
            //4.遍历结果集
            if(resultSet.next()){
                User user = new User();
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));
                user.setUserId(resultSet.getInt("userId"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    //3.按照ID查找用户信息
    public User selectById(int userId){
        //1.建立连接
        Connection connection = DBUtil.getConnection();
        //2.拼装sql语句
        String sql = "select * from user where userId = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            //3.执行sql
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    /*public static void main(String[] args) {
        UserDao userDao = new UserDao();
        //1.测试add方法
        User user = new User();
        user.setName("yx");
        user.setPassword("1234");
        userDao.add(user);

        //2.测试selectByName方法
        //System.out.println(userDao.selectByName("yx"));

        //3.测试selectById方法
        //userDao.selectById(1);
    }*/
}
